//
//  TMMerchantCollectController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/29.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMMerchantCollectController.h"
#import "TMMerchantCollectCell.h"
#import "TMFootprintCell.h"
#import "TMCheckActionView.h"
#import "TMUserViewModel.h"
#import "TMCollectionModel.h"
#import "TMCommodityDetailController.h"
#import "TMShopHomeController.h"
#import "WZNoResultView.h"
@interface TMMerchantCollectController ()
@property (nonatomic, strong) TMUserViewModel *viewModel;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) TMCheckActionView *checkActionView;
@property (nonatomic, strong) WZNoResultView *noResultView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger totalNum;
@end

@implementation TMMerchantCollectController

-(NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = !self.isFoot ? @"商家收藏" :@"足迹";
    //[self displayRightItem];
    [self.tableView registerNib:[UINib nibWithNibName:@"TMMerchantCollectCell" bundle:nil] forCellReuseIdentifier:@"TMMerchantCollectCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TMFootprintCell" bundle:nil] forCellReuseIdentifier:@"TMFootprintCell"];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.viewModel = [[TMUserViewModel alloc]init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (!self.selectIndex) {
            self.selectIndex = 1;
            //[[NSUserDefaults standardUserDefaults] integerForKey:@"indexss"]
        }
        self.pageNum = 1;
        if (self.isFoot == NO) {
            [self.viewModel getShopCollectionAndPage:self.pageNum  Success:^(id responseObject, id total) {
                self.totalNum = [total integerValue];
                self.listArray = responseObject;
                [self noResultViewShowORNot];
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
                [self.tableView reloadData];
            } failed:^(NSString *error) {
                [self.tableView.mj_header endRefreshing];
                 [self.tableView.mj_footer endRefreshing];
                 [self noResultViewShowORNot];
                [self.tableView reloadData];
            }];
        }else{
            [self.viewModel getFooterCollection:self.selectIndex Page:self.pageNum  Success:^(id responseObject ,id total) {
                self.listArray = responseObject;
                self.totalNum = [total integerValue];
                 [self noResultViewShowORNot];
                [self.tableView.mj_header endRefreshing];
                 [self.tableView.mj_footer endRefreshing];
                [self.tableView reloadData];
            } failed:^(NSString *error) {
                [self.tableView.mj_header endRefreshing];
                 [self.tableView.mj_footer endRefreshing];
                 [self noResultViewShowORNot];
                [self.tableView reloadData];
            }];
        }
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.pageNum ++;
        if (self.pageNum > self.totalNum) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        if (self.isFoot == NO) {
            [self.viewModel getShopCollectionAndPage:self.pageNum  Success:^(id responseObject, id total) {
                self.listArray = responseObject;
                [self.tableView.mj_header endRefreshing];
                 [self.tableView.mj_footer endRefreshing];
                [self.tableView reloadData];
            } failed:^(NSString *error) {
                [self.tableView.mj_header endRefreshing];
                 [self.tableView.mj_footer endRefreshing];
                if (self.pageNum > 1) {
                    self.pageNum--;
                }
                [self.tableView reloadData];
            }];
        }else{
            [self.viewModel getFooterCollection:self.selectIndex Page:self.pageNum Success:^(id responseObject, id total) {
                self.listArray = responseObject;
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
                [self.tableView reloadData];
            } failed:^(NSString *error) {
                if (self.pageNum > 1) {
                    self.pageNum--;
                }
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
                [self.tableView reloadData];
            }];
        }
    }];
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TMFootprintCell *footCell = [tableView dequeueReusableCellWithIdentifier:@"TMFootprintCell"];
    TMMerchantCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMMerchantCollectCell"];
    if (self.isFoot == NO) {
        cell.model = self.listArray[indexPath.row];
        return cell;
    }else{
        if (self.selectIndex == 1) {
           cell.model = self.listArray[indexPath.row];
            return cell;
        }else{
            footCell.courseModel = self.listArray[indexPath.row];
            return footCell;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 96;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.isFoot == NO) {
        return [UIView new];
    }
    _checkActionView =  [[NSBundle mainBundle] loadNibNamed:@"TMCheckActionView" owner:self options:nil].lastObject;
    if (self.isFoot == YES) {
        [_checkActionView.fristBTN setTitle:@"机构" forState:UIControlStateNormal];
        [_checkActionView.secondBtn setTitle:@"课程" forState:UIControlStateNormal];
    }
    __weak typeof(self) weakself = self;
    _checkActionView.block = ^(NSInteger index) {
        NSLog(@"%ld",index);
        [[NSUserDefaults standardUserDefaults] setInteger:index+1 forKey:@"indexss"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        weakself.selectIndex = index +1;
        [weakself.tableView.mj_header beginRefreshing];
    };
    return _checkActionView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.isFoot == YES) {
        return  74;
    }else{
        return 0.01f;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isFoot == NO) {
        TMCollectionModel *model = self.listArray[indexPath.row];
        TMShopHomeController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMShopHomeController"];
        vc.shopid = model.collid;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
    if (self.selectIndex == 1) {
        TMCollectionModel *model = self.listArray[indexPath.row];
        TMShopHomeController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMShopHomeController"];
        vc.shopid = model.cid;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (self.selectIndex == 2) {
        TMCollectionModel *model = self.listArray[indexPath.row];
        TMCommodityDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCommodityDetailController"];
        vc.rid = model.cid;
        [self.navigationController pushViewController:vc animated:YES];
    }
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.isFoot ? @"删除足迹": @"取消收藏";
}
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return UITableViewCellEditingStyleDelete;
//}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (self.isFoot == NO) {
            TMCollectionModel *model = self.listArray[indexPath.row];
            [self.listArray removeObject:model];
            [self.viewModel deleteShopCollection:model.collid Success:^(id responseObject) {
                [self.tableView.mj_header beginRefreshing];
            } failed:^(NSString *error) {
                
            }];
            
        }else{
            TMCollectionModel *model = self.listArray[indexPath.row];
            [self.listArray removeObject:model];
            [self.viewModel deleteFooterCollection:model.collid Success:^(id responseObject) {
                [self.tableView.mj_header beginRefreshing];
            } failed:^(NSString *error) {
                
            }];
        }
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    [tableView reloadData];
    [self noResultViewShowORNot];
}
- (WZNoResultView *)noResultView{
    if (!_noResultView) {
        _noResultView = [[[NSBundle mainBundle] loadNibNamed:@"WZNoResultView" owner:nil options:nil]firstObject];
        if (self.isFoot == YES) {
            _noResultView.frame = CGRectMake(0, 74, kWindowW, kWindowH-74);
        }else {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }
        _noResultView.isSearchVC = YES;
        [self.tableView addSubview:_noResultView];
    }
    return _noResultView;
}
- (void)noResultViewShowORNot{
    if (!self.listArray.count) {
        self.noResultView.hidden = NO;
    }else{
        self.noResultView.hidden = YES;
    }
}

@end
